// Package system
// @Author zhongxc
// @Date 2024/7/26 16:43:00
// @Desc
package system

import (
	"context"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"google.golang.org/protobuf/types/known/emptypb"
	"net/http"
	"strconv"
	"strings"
	"vben-api/api/rpc"
	"vben-api/api/types"
	"vben-api/pkg/svc"
	common "vben-common"
	"vben-common/errs"
	"vben-common/utils"
	interface_service_v1 "vben-grpc-common/vben-admin/types/interface.service.v1"
)

type Interfaces struct {
	svcCtx *svc.ServiceContext
}

func NewInterfaces(svcCtx *svc.ServiceContext) *Interfaces {
	return &Interfaces{
		svcCtx: svcCtx,
	}
}

// GetInterfaceInfo
// @Description: 接口详情
// @receiver i
// @param c
func (i *Interfaces) GetInterfaceInfo(c *gin.Context) {
	r := common.NewResult()
	id, err := strconv.ParseInt(c.Param("id"), 10, 64)
	if err != nil {
		c.JSON(http.StatusOK, r.Fail(400, "id参数不合法"))
		return
	}

	in := &interface_service_v1.GetInterfaceInfoRequest{
		Id: id,
	}
	info, err := rpc.InterfaceServiceClient.GetInterfaceInfo(context.Background(), in)
	if err != nil {
		code, msg := errs.ParseGrpcError(err)
		c.JSON(http.StatusOK, r.Fail(code, msg))
		return
	}

	var interfaceEntity types.InterfaceEntity
	_ = copier.Copy(&interfaceEntity, info)

	c.JSON(http.StatusOK, r.Success(interfaceEntity))
}

func (i *Interfaces) GetInterfaceList(c *gin.Context) {
	r := common.NewResult()
	// 参数获取
	page, err := strconv.ParseInt(c.DefaultQuery("page", "1"), 10, 64)
	if err != nil {
		c.JSON(http.StatusOK, r.Fail(400, "页码错误"))
		return
	}
	pageSize, err := strconv.ParseInt(c.DefaultQuery("pageSize", "10"), 10, 64)
	if err != nil {
		c.JSON(http.StatusOK, r.Fail(400, "limit参数错误"))
		return
	}
	name := c.DefaultQuery("name", "")
	url := c.DefaultQuery("url", "")

	// 参数组装
	in := &interface_service_v1.GetInterfaceListRequest{
		Page:     page,
		PageSize: pageSize,
		Name:     name,
		Url:      url,
	}

	// 发起列表查询请求
	list, err := rpc.InterfaceServiceClient.GetInterfaceList(context.Background(), in)
	if err != nil {
		code, msg := errs.ParseGrpcError(err)
		c.JSON(http.StatusOK, r.Fail(code, msg))
		return
	}

	// 数据转换
	var interfaceEntityList = make([]types.InterfaceEntity, 0)
	for _, item := range list.Data {
		var interfaceEntity types.InterfaceEntity
		_ = copier.Copy(&interfaceEntity, item)
		interfaceEntityList = append(interfaceEntityList, interfaceEntity)
	}

	// 响应数据组装
	resultMap := map[string]interface{}{
		"page":      list.Page,
		"pageSize":  list.Limit,
		"totalPage": list.TotalPage,
		"total":     list.TotalCount,
		"items":     interfaceEntityList,
	}

	c.JSON(http.StatusOK, r.Success(resultMap))
}

// SaveInterface
// @Description: 创建接口
// @receiver i
// @param c
func (i *Interfaces) SaveInterface(c *gin.Context) {
	r := common.NewResult()
	var interfaceEntity types.InterfaceEntity
	if err := c.ShouldBind(&interfaceEntity); err != nil {
		c.JSON(http.StatusOK, r.Fail(400, "参数错误"))
		return
	}

	var in interface_service_v1.CreateInterfaceRequest
	_ = copier.Copy(&in, interfaceEntity)
	in.CreatedBy = "Kkk"

	res, err := rpc.InterfaceServiceClient.CreateInterface(context.Background(), &in)
	if err != nil {
		code, msg := errs.ParseGrpcError(err)
		c.JSON(http.StatusOK, r.Fail(code, msg))
		return
	}
	c.JSON(http.StatusOK, r.Success(res))
}

// UpdateInterface
// @Description: 接口更新
// @receiver i
// @param c
func (i *Interfaces) UpdateInterface(c *gin.Context) {
	r := common.NewResult()
	id, err := strconv.ParseInt(c.Param("id"), 10, 64)
	if err != nil {
		c.JSON(http.StatusOK, r.Fail(400, "id参数不合法"))
		return
	}

	var interfaceEntity types.InterfaceEntity
	if err := c.ShouldBind(&interfaceEntity); err != nil {
		c.JSON(http.StatusOK, r.Fail(400, "参数错误"))
		return
	}
	var in interface_service_v1.UpdateInterfaceRequest
	_ = copier.Copy(&in, interfaceEntity)
	in.Id = id
	in.UpdatedBy = "Kkk"

	res, err := rpc.InterfaceServiceClient.UpdateInterface(context.Background(), &in)
	if err != nil {
		code, msg := errs.ParseGrpcError(err)
		c.JSON(http.StatusOK, r.Fail(code, msg))
		return
	}
	c.JSON(http.StatusOK, r.Success(res))
}

// DeleteInterface
// @Description: 删除接口
// @receiver i
// @param c
func (i *Interfaces) DeleteInterface(c *gin.Context) {
	r := common.NewResult()
	ids := c.PostForm("ids")
	if strings.TrimSpace(ids) == "" {
		c.JSON(http.StatusOK, r.Fail(400, "ids参数错误"))
		return
	}
	idsStr := strings.Split(ids, ",")
	idSlice, err := utils.StringSliceToIntSlice(idsStr)
	if err != nil {
		c.JSON(http.StatusOK, r.Fail(400, "ids参数错误"))
		return
	}

	in := &interface_service_v1.DeleteInterfaceRequest{
		Ids: idSlice,
	}

	res, err := rpc.InterfaceServiceClient.DeleteInterface(context.Background(), in)
	if err != nil {
		code, msg := errs.ParseGrpcError(err)
		c.JSON(http.StatusOK, r.Fail(code, msg))
		return
	}

	c.JSON(http.StatusOK, r.Success(res))
}

func (i *Interfaces) GetAll(c *gin.Context) {
	r := common.NewResult()
	list, err := rpc.InterfaceServiceClient.GetAll(context.Background(), &emptypb.Empty{})
	if err != nil {
		code, msg := errs.ParseGrpcError(err)
		c.JSON(http.StatusOK, r.Fail(code, msg))
		return
	}

	var interfaceEntityList = make([]types.InterfaceEntity, 0)
	for _, item := range list.Data {
		var interfaceEntity types.InterfaceEntity
		_ = copier.Copy(&interfaceEntity, item)
		interfaceEntityList = append(interfaceEntityList, interfaceEntity)
	}

	c.JSON(http.StatusOK, r.Success(interfaceEntityList))
}
